/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GameView;

import Car.Car;
import java.util.Vector;
import javax.microedition.lcdui.*;

/**
 * @author Sambya
 */
public class RCCanvas extends Canvas
        implements Runnable {
    private final int CarWidth= 12;
    private Vector Data;
    /**
     * constructor
     */
    public RCCanvas(Vector _data) {
        // Set up this canvas to listen to command events
        // Add the Exit command
       //System.out.println("DEBUG: Size: "+_data.size());
        Data = _data;
    }
    
    /**
     * menentukan warna saat kecepatan v
     * @param v suatu kecepatan
     * @return warna RGB (0xRRGGBB)
     */
    public int MakeColor (double v) {
        int gr= Math.max(255 - (int) ((255/15)*v), 0 );
        int re= Math.max((int) ((255/15)*(v-15)), 0 );
        int bl=0;
        if (v>5&&v<20){
            if (v < 15){
                bl = (int)(255/10*(v-5));
            } else {
                bl = 255-(int)(255/10*(v-15));
            }
        }
        
        int retval = re<<16|gr<<8|bl;
        return retval;
    }
    
    /**
     * paint
     */
    public void paint(Graphics g) {
        int w = getWidth(), h = getHeight();
        g.setGrayScale(255);
        g.fillRect(0, 0, w, h);
        g.setGrayScale(0);
        g.drawString("Mobil Gerak V2", w/2, 0, Graphics.TOP | Graphics.HCENTER);
        g.drawString("Jumlah Mobil : "+Data.size(),w/2, h-20, Graphics.TOP | Graphics.HCENTER);
        g.drawLine( 10, h-5, 10, 5);
        g.drawLine( w-10, h-5, w-10, 5);
        for (int i=0;i<Data.size();++i) {
            Car x = (Car)Data.elementAt(i);
            g.setColor(MakeColor(x.getVel()));
            int xx = CarWidth+5+ (int) ((x.getPos().getAbsis())*(2*CarWidth));
            int yy = h-(CarWidth) - (int) ((x.getPos().getOrdinat())*CarWidth);
            g.fillTriangle(xx, yy, xx+CarWidth/2, yy-CarWidth, xx+CarWidth, yy);
        }
                
    }

    public void run() {
    }




}
